
(function(){

  window.resetdata = false;

  var techOrder = [
    'comment',
    'html', 
    'bemjson.js', 'bemhtml', 'bemdecl.js',
    'css', 'ie.css', 'xsl',
    'js',
    'json', 'xml',
    'png', 'gif', 'jpg'
  ];

  App.Type.TechType.all.sync([
    {
      id: 'comment',
      title: 'Описание'
    },
    {
      id: 'css',
      title: 'CSS',
      template: '.{name}\n{\n{indent}|\n}'
    },
    {
      id: 'ie.css',
      title: 'CSS/IE',
      template: '.{name}\n{\n{indent}|\n}'
    },
    {
      id: 'js',
      title: 'Javascript',
      template: '$(\'.{name}\').|'
    },
    {
      id: 'html',
      title: 'HTML',
      template: '<div class="{name}">\n{indent}|\n</div>'
    },
    {
      id: 'bemhtml',
      title: 'bemHTML'
    },
    {
      id: 'bemjson.js',
      title: 'bemJSON',
      template: '({\n{indent}block: \'b-page\',\n{indent}title: \'Page title\',\n{indent}content: {\n{indent}{indent}|\n{indent}}\n})'
    },
    {
      id: 'bemdecl.js',
      title: 'BEM декларация',
      template: 'exports.blocks = [\n    |\n];'
    },
    {
      id: 'json',
      title: 'JSON'
    },
    {
      id: 'txt',
      title: 'Text'
    },
    {
      id: 'xml',
      title: 'XML'
    },
    {
      id: 'xsl',
      title: 'XSL'
    },
    {
      id: 'png',
      title: 'png',
      isBinary: true
    },
    {
      id: 'gif',
      title: 'gif',
      isBinary: true
    },
    {
      id: 'jpg',
      title: 'jpg',
      isBinary: true
    }
  ].map(function(type){
    type.order = (techOrder.indexOf(type.id) + 1) || 1000;
    return type;
  }));

  if (window.localStorage && window.JSON)
  {
    Basis.Event.onLoad(function(){
      if (window.localStorage['BEMData'])
      {
        // load here
        Object.iterate(JSON.parse(localStorage['BEMData']), function(type, data){
          App.Type[type].all.sync(data);
        });
      }
      else
        // load demo project
        Basis.DOM.tag('HEAD')[0].appendChild(Basis.DOM.createElement('SCRIPT[type="text/javascript"][src="app/_static/init.project.js"]'));
    });

    Basis.Event.onUnload(function(){
      if (!window.resetdata)
      {
        var storedata = {};

        'CommandRun Level Block Elem Mod Tech'.qw().forEach(function(type){
          this[type] = App.Type[type].all.getItems().map(Function.getter('data'));
        }, storedata);

        localStorage['BEMData'] = JSON.stringify(storedata);
      }
      else
        localStorage.removeItem('BEMData');
    });
  }

})();